﻿using ExternalModels;
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FluentValidationWebApi.Validators
{
    public class DeveloperValidator : AbstractValidator<Developer>
    {
        public DeveloperValidator()
        {
            RuleFor(p => p.UserName)
                .Cascade(CascadeMode.Stop)
                .NotEmpty()
                .WithMessage("{PropertyName} 属性不能为空！")
                .Length(2, 25) //长度范围
                .Must(IsValidName).WithMessage("{PropertyName} 属性均为字母！");

            RuleFor(p => p.Mobile)
                .Length(11).WithMessage("长度不正确");
        }

        //自定义验证规则
        private bool IsValidName(string name)
        {
            return name.All(Char.IsLetter);
        }
    }
}
